package com.example.jin.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.util.List;
import java.util.UUID;

/**
 * Created by jin on 2016/5/30.
 */
public class CrimePagerActivity extends AppCompatActivity implements CrimeFragment.Callback{
    private ViewPager mViewPager;
    private List<Crime> mCrimes;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crime_page);

        mViewPager=(ViewPager)findViewById(R.id.viewpager);
        mCrimes=CrimeList.Instance(CrimePagerActivity.this).getCrimes();

        FragmentManager manager=getSupportFragmentManager();
        //注意FragmentStatePagerAdapter和FragmentPagerAdapter之间的区别（Fragment是否被销毁（View保留））
        mViewPager.setAdapter(new FragmentStatePagerAdapter(manager) {
            @Override
            public Fragment getItem(int position) {
                Crime crime=mCrimes.get(position);
                Fragment fragment=new CrimeFragment();
                Bundle bundle=new Bundle();
                bundle.putSerializable("crimeid",crime.getId());
                fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getCount() {
                return mCrimes.size();
            }
        });
        mViewPager.setCurrentItem(CrimeList.Instance(CrimePagerActivity.this).getPosition((UUID)getIntent().getSerializableExtra("crimeid")));
    }

    @Nullable
    @Override
    public Intent getParentActivityIntent() {
        /**
         * 在manifests中设置了parentActivityName，因此能够得到parent
         * 这里获取Parent传入的Intent，从而获得传入的参数
         */
        Log.i("main","getParentActivityIntent");
        Intent intent= super.getParentActivityIntent();
        intent.putExtra("subtitle",true);
        return intent;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.crime_delete_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_delete_crime: {

                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemUpdate(Crime c) {

    }
}
